package com.jianyun.jyzs.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.common.HttpResultFileVO;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.http.AsyTask.AsyncTaskActDownFileUtil;
import com.jianyun.jyzs.utils.FileUtil;
import com.jianyun.jyzs.utils.LoginCache;

import java.io.File;

/**
 * Created by liml on 17/5/15.
 */

public class ShowFileActivity extends Activity{
    private RelativeLayout loadingLayout;
    private ProgressBar progressBar;
    private String localFilePath;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            HttpResultFileVO vo = null;
            if(msg.getData() != null){
                Bundle bundle = msg.getData();
                vo = (HttpResultFileVO)bundle.getSerializable(SysConstant.msg_key_parcelable);
            }
            if(vo != null && !vo.isNormal){
                Toast.makeText(getBaseContext(), vo.err, Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(getBaseContext(), "文件保存至"+ FileUtil.getFileFolder(vo.filePath)+"文件夹", Toast.LENGTH_SHORT).show();
            switch (msg.what) {
                case SysConstant.FILE:
                    loadingLayout.setVisibility(View.GONE);
                    progressBar.setProgress(0);
                    showLocalFile(vo.filePath,vo.id);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_show_video);

        loadingLayout = (RelativeLayout) findViewById(R.id.loading_layout);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setMax(100);

        localFilePath = getIntent().getStringExtra("localpath");
        int type = getIntent().getIntExtra("type",0);
        Integer id = getIntent().getIntExtra("id",0);
        String fileName = getIntent().getStringExtra("fileName");
        String folderID = getIntent().getStringExtra("folderID");
        String remotepath = getIntent().getStringExtra("remotepath");

        if (localFilePath != null && new File(localFilePath).exists()) {
            showLocalFile(localFilePath,null);
        } else if (!TextUtils.isEmpty(remotepath) && !remotepath.equals("null")) {
            downloadFile(remotepath,type,id,new String[]{fileName,folderID});
        }
    }

    /**
     * 播放本地视频
     * @param localPath 视频路径
     */
    private void showLocalFile(String localPath,Integer id){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(localPath)),FileUtil.getAppType("."+FileUtil.getFileExtName(localPath)));
        try {
            startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(this, "文件打开失败,无打开此文件的应用", Toast.LENGTH_SHORT).show();
        }

        if(id != null){
            Intent mIntent = new Intent();
            mIntent.putExtra("id", id);
            mIntent.putExtra("filepath", localPath);
            setResult(RESULT_OK, mIntent);
        }
        finish();
    }

    /**
     * 下载文件
     */
    private void downloadFile(String remoteUrl,int type,int id,String[] str) {
        loadingLayout.setVisibility(View.VISIBLE);
        AsyncTaskActDownFileUtil atastk = new AsyncTaskActDownFileUtil();
        String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
        if(remoteUrl.startsWith("http")){
            atastk.execute(mHandler, remoteUrl,type , progressBar,SysConstant.FILE,id,str);
        }else{
            atastk.execute(mHandler, erpRootUrl + remoteUrl,type , progressBar,SysConstant.FILE,id,str);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
